<?php
class Admin_PermissionController extends Ctrl_Admin{
    public function indexAction(){
        $p = $_REQUEST;
        $pRole = empty($p['role'])?1:Tool_Fnc::safe_string($p['role']);

        $tPMO = new PermissionModel;

        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pRoleid = empty($p['role_id'])?'':Tool_Fnc::safe_string($p['role_id']);
            $pRole = empty($p['role'])?1:Tool_Fnc::safe_string($p['role']);
            $pPermission = empty($p['permission'])?'':implode(',',$p['permission']);
            $tTime = time();
            
            $tPData = $tPMO->field('id')->where('role = '.$pRole)->fRow();
            if(!empty($pRoleid)){
                $tPData = $tPMO->field('permission')->where('role = '.$pRoleid)->fRow();
                Tool_Fnc::ajaxMsg('',1,$tPData);
            }
            if(empty($tPData['id'])){
                $tData = array(
                    'role' => $pRole,
                    'permission' => $pPermission,
                    'created' => $tTime,
                );
                if(!$tId = $tPMO->insert($tData)){
                    Tool_Fnc::ajaxMsg('修改失败');
                }
                Tool_Fnc::ajaxMsg('修改成功',1);   
            }else{

                $tData = array(
                    'role' => $pRole,
                    'permission' => $pPermission,
                    'updated' => $tTime,
                    'id' => $tPData['id'],
                );
                if(!$tPMO->update($tData)){
                    Tool_Fnc::ajaxMsg('修改失败 002');
                }
                Tool_Fnc::ajaxMsg('修改成功',1);   
            }
        }

        //角色信息展示
        $tRIMO = new RoleinfoModel;
        $tRIDatas = $tRIMO->field('num,introduce')->fList();
        //权限展示
        $tPIMO = new PermissioninfoModel;
        $tPIDatas = $tPIMO->field('num,introduce')->fList();
        //默认显示超级管理员权限
        $tPRow = $tPMO->field('permission')->where('role = '.$pRole)->fRow();

        $this->assign('tPRow',$tPRow);
        $this->assign('tRDatas',$tRIDatas);
        $this->assign('tPIDatas',$tPIDatas);
    
    }
    public function editAction(){
        $p = $_REQUEST;
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            $pRole = empty($p['role'])?'':Tool_Fnc::safe_string($p['role']);
            $tPMO = new PermissionModel;
            $tData = $tPMO->field('permission')->where('role = '.$pRole)->fRow();
            if(empty($tData['permission'])){
                Tool_Fnc::ajaxMsg('记录为空');
            }
            Tool_Fnc::ajaxMsg('',1,$tData);
        }     
    }
}
?>
